import { DatePicker } from 'antd';
import { useDispatch } from 'dva';
import moment from 'moment';
import React, { useState } from 'react';
import { ISurveyHook } from '../../../utils/Survey/SurveyHook';
import AuthCompount from '../../AuthCompount/AuthCompount';
import './FormDate.scss';
const isPc = window.screen.width > 500;
// tslint:disable-next-line:no-any
declare var window: any;
/**
 * 日期选择
 * 在subjectData里面type为FormDate
 * 具体用法参照 ant design
 */
function FormDate(props: ISurveyHook) {
  const dispatch = useDispatch();
  const [val, setVal] = useState(props.formObj.subjectData.defaultValue);
  function inputChange(date: moment.Moment | null, dateString: string) {
    dispatch({
      type: 'survey/changeForm',
      payload: {
        props,
        val: dateString,
      },
    });
    setVal(dateString);
  }

  /**
   * 用户点击回显上次方式的监听
   * 直接触发当前组件的渲染
   */
  // tslint:disable-next-line:no-any
  window[`onlastSurvey_${props.formObj.subjectData.field}`] = (value: any) => {
    setVal(value);
  };

  return (
    <div id="FormDate">
      <div className="form_box">
        <div className="tit_box">
          <AuthCompount isShow={!!props.formObj.validatorData.required}>
            <div
              // tslint:disable-next-line:jsx-no-multiline-js
              className={props.formObj.subjectData.errMsg ? 'cl star' : 'star'}
            >
              *
            </div>
          </AuthCompount>
          <div className="cln">{props.formObj.subjectData.subjectName}</div>
          <AuthCompount isShow={!!props.formObj.subjectData.explain}>
            <div className="explain">（说明：{props.formObj.subjectData.explain}）</div>
          </AuthCompount>
        </div>
        <DatePicker
          className={`form_input`}
          disabled={props.formObj.subjectData.disable}
          // tslint:disable-next-line:jsx-no-multiline-js
          defaultValue={
            props.formObj.subjectData.defaultValue ? moment(props.formObj.subjectData.defaultValue) : undefined
          }
          value={val ? moment(val) : undefined}
          onChange={inputChange}
          getCalendarContainer={() => document.getElementById('scrl_box')!}
        />
        <AuthCompount isShow={isPc}>
          <div className="errMsg">{props.formObj.subjectData.errMsg}</div>
        </AuthCompount>
      </div>
      <AuthCompount isShow={!isPc}>
        <div className="m_errMsg">{props.formObj.subjectData.errMsg}</div>
      </AuthCompount>
    </div>
  );
}
export default FormDate;
